package com.bobi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bobi.entity.CutpriceActivityGoods;
import com.bobi.entity.PromotionGoods;
import com.bobi.enums.ResultCode;
import com.bobi.service.CutpriceActivityGoodsService;
import com.bobi.service.GoodsService;
import com.bobi.service.PromotionGoodsService;
import com.bobi.service.PromotionService;
import com.bobi.utils.PageUtil;
import com.bobi.utils.ResultUtil;
import com.bobi.vo.CutpriceActivityGoodsVO;
import com.bobi.vo.PageVO;
import com.bobi.vo.ResultMessage;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wenyuan
 * @version 1.0
 * @date 2023/7/4
 * @since 8
 */
@RestController
@RequestMapping("/cutpricegoods")
public class CutPriceActivityGoodsController {

    @Autowired
    private PromotionGoodsService promotionGoodsService;

    @Autowired
    private CutpriceActivityGoodsService cutpriceActivityGoodsService;

    @Autowired
    private PromotionService promotionService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询所有信息
     * @param id 活动id
     * @return 返回分页对象
     */
    @GetMapping(value=  "/{id}")
    public ResultMessage<Page<CutpriceActivityGoodsVO>> getPromotionGoods(@PathVariable("id") String id, PageVO pageVO){
        if(id == null) {
            return ResultUtil.error(ResultCode.PROMOTION_SEARCH_ERROR);
        }
        // 查询促销商品的id列表
        QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("promotion_id", id);
        List<PromotionGoods> promotionGoods = promotionGoodsService.list(queryWrapper);
        List<Integer> ids = promotionGoods.stream().map(PromotionGoods::getGoodsId).collect(Collectors.toList());
        // 根据用户id查砍价商品表
        QueryWrapper<CutpriceActivityGoods> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id", ids);
        List<CutpriceActivityGoods> list = cutpriceActivityGoodsService.list(queryWrapper1);
        // 设置促销商品信息
        ArrayList<CutpriceActivityGoodsVO> arrayList = new ArrayList<>(list.size());
        list.forEach(i ->{
            CutpriceActivityGoodsVO cutpriceActivityGoodsVO = new CutpriceActivityGoodsVO();
            cutpriceActivityGoodsVO.setCutpriceActivityGoods(i);
            arrayList.add(cutpriceActivityGoodsVO);
        });
        // 设置促销商品对应的更全面的信息
        arrayList.forEach(i ->{
            i.setGoods(goodsService.getById(i.getCutpriceActivityGoods().getGoodsId()));
        });
        // 将列表转为分页对象
        Page<CutpriceActivityGoodsVO> page = new Page<CutpriceActivityGoodsVO>(pageVO.getPageNumber(), pageVO.getPageSize());
        page.setRecords(arrayList);
        page.setTotal(arrayList.size());
        return ResultUtil.data(page);
    }

    /**
     * 分页查询所有信息
     * @param pageVO 分页对象
     * @return 返回分页对象
     */
    @GetMapping(value=  "/page/{ids}")
    public ResultMessage<IPage<CutpriceActivityGoodsVO>> page(@PathVariable List ids, @RequestBody CutpriceActivityGoods cutpriceActivityGoods, PageVO pageVO){
        if(cutpriceActivityGoods == null){
            return ResultUtil.error(ResultCode.PROMOTION_SEARCH_ERROR);
        }
        if(ids == null && ids.isEmpty() && ids.contains(cutpriceActivityGoods.getId())){
            return ResultUtil.error(ResultCode.PROMOTION_UPDATE_ERROR);
        }
        QueryWrapper<CutpriceActivityGoods> queryWrapper = cutpriceActivityGoods.initWrapper(cutpriceActivityGoods);
        Page<CutpriceActivityGoods> page = PageUtil.initPage(pageVO);
        Page<CutpriceActivityGoods> list;
        if(queryWrapper == null){
            list = cutpriceActivityGoodsService.page(page);
        }else{
            list = cutpriceActivityGoodsService.page(page, queryWrapper);
        }
        List<CutpriceActivityGoodsVO> collect = list.getRecords().stream().map(CutpriceActivityGoodsVO::new).collect(Collectors.toList());
        IPage<CutpriceActivityGoodsVO> cutpriceActivityGoodsVOIPage = PageUtil.convertPage(list, collect);
        return ResultUtil.data(cutpriceActivityGoodsVOIPage);
    }

    /**
     * 添加活动
     * @param cutpriceActivityGoods 促销活动商品对象
     * @return 返回的标志
     */
    @PostMapping
    public ResultMessage<Object> addActivity(CutpriceActivityGoods cutpriceActivityGoods, Integer promotionId){
        // 如果删除成功
        cutpriceActivityGoodsService.save(cutpriceActivityGoods);
        PromotionGoods promotionGoods = new PromotionGoods();
        promotionGoods.setGoodsId(Integer.parseInt(cutpriceActivityGoods.getId()));
        promotionGoods.setPromotionId(promotionId);
        promotionGoodsService.save(promotionGoods);
        return ResultUtil.success();
    }

    /**
     * 更新活动，不允许修改所属活动
     * @param cutpriceActivityGoods 活动商品对象
     * @return 返回对象
     */
    @PutMapping
    public ResultMessage<Object> updateActivity(CutpriceActivityGoods cutpriceActivityGoods){
        // 构造查询条件包装
        UpdateWrapper<CutpriceActivityGoods> updateWrapper = new UpdateWrapper<CutpriceActivityGoods>().eq("id", cutpriceActivityGoods.getId());
        // 如果更新成功
        cutpriceActivityGoodsService.update(cutpriceActivityGoods, updateWrapper);
        return ResultUtil.success();
    }

    /**
     * 删除商品(逻辑删除)
     * @param id 活动id
     * @return 结果消息
     */
    @DeleteMapping("/{id}")
    public ResultMessage<Object>  deleteActivity(@PathVariable("id") String id){
        if(id == null){
            return ResultUtil.error(ResultCode.PROMOTION_DELETE_ERROR);
        }
        cutpriceActivityGoodsService.removeById(id);
        promotionGoodsService.remove(new QueryWrapper<PromotionGoods>().eq("goods_id", id));
        return ResultUtil.success();
    }

    @DeleteMapping
    public ResultMessage<Object>  deleteBatchActivity(@RequestParam List<String> ids){
        if(ids == null){
            return ResultUtil.error(ResultCode.PROMOTION_DELETE_ERROR);
        }
        cutpriceActivityGoodsService.removeBatchByIds(ids);
        ids.forEach(id->{
            promotionGoodsService.remove(new QueryWrapper<PromotionGoods>().eq("goods_id", id));
        });
        return ResultUtil.success();
    }


}

